﻿using AppExample;
using Newtonsoft.Json;
using OAuth;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace SafeTraveller.Controllers
{
    public class HomeController : Controller
    {
        private const String ConsumerKey = "ay6t0YBkUs5Qm0mGQSYCqg";
        private const String ConsumerSecret = "WKztDXhDZXXOflRX4kMFMiFZMxpA3GRamymvfoqOI";
        private const String tmpQuery = "https://api.twitter.com/1.1/search/tweets.json?q=include_entities=epidemic";


        public static string CalculateSHA1(string text, Encoding enc)
        {
            byte[] buffer = enc.GetBytes(text);
            SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
            return BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "").ToLower();
        }

        private void UploadData(){
            FileStream fs = new FileStream(ConfigurationSettings.AppSettings["file"], FileMode.Open, FileAccess.Read);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();

            // Generate post objects
            Dictionary<string, object> postParameters = new Dictionary<string, object>();
            postParameters.Add("filename", "data.csv");
            postParameters.Add("fileformat", "csv");
            postParameters.Add("file", new FormUpload.FileParameter(data, "People.csv", "application/vnd.ms-excel"));

            // Create request and receive response
            string postURL = ConfigurationSettings.AppSettings["url"] + "api?s=ImportService&withheader=1&name=data&delimiter=,";
            //"country","series","Unit","Date","Value"

            List<Field> fields = new List<Field>();
            fields.Add(new Field() { Name = "date", TypeName = "timestamp" });
            fields.Add(new Field() { Name = "territory", TypeName = "string" });
            fields.Add(new Field() { Name = "count", TypeName = "double" });
            fields.Add(new Field() { Name = "risk", TypeName = "string" });
            postURL += "&fields=" + JsonConvert.SerializeObject(fields);
            string userAgent = "Someone";
            HttpWebResponse webResponse = FormUpload.MultipartFormDataPost(postURL,
                userAgent,
                postParameters,
                CalculateSHA1(ConfigurationSettings.AppSettings["secret"] + ConfigurationSettings.AppSettings["token"], Encoding.UTF8),
                int.Parse(ConfigurationSettings.AppSettings["appid"]));

            // Process response
            StreamReader responseReader = new StreamReader(webResponse.GetResponseStream());
            string fullResponse = responseReader.ReadToEnd();
            webResponse.Close();
            //info.Text = fullResponse;

            Schema s = JsonConvert.DeserializeObject<Schema>(fullResponse);

            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ButtonClickScript", "CI.openSource(" + s.Key + ");", true);
        }
        
        public ActionResult Index()
        {
            
            

            /*var rawResult =
                (from raw in twitterCtx.RawQuery
                 where raw.QueryString == "account/totals.json"
                 select raw)
                .FirstOrDefault();*/

            return View();
        }

        

       
    }
}
